package com.cqq.attachment.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqq.attachment.dao.AttachmentDao;
import com.cqq.attachment.dao.domain.Attachment;
import com.cqq.attachment.service.bean.AttachmentBean;
import com.cqq.attachment.service.exception.AttachmentException;
import com.cqq.common.core.constant.CommonMessage;
import com.cqq.common.core.util.Assert;


/**
 * @author generated
 * @date 2015-12-03
 */


@Service(value="attachmentService")
public class AttachmentServiceImpl implements com.cqq.attachment.service.AttachmentService{

             @Autowired
             private AttachmentDao attachmentDao;

			
			public int deleteByPrimaryKey(Integer id) {
				return attachmentDao.deleteByPrimaryKey(id);
			}
		    
			
			public int insertSelective(AttachmentBean bean) throws AttachmentException {
				Assert.notNull(bean, new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				Assert.notNull(bean.getOriginFileName(), new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				Assert.notNull(bean.getFileName(), new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				Assert.notNull(bean.getFilePath(), new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				Assert.notNull(bean.getFileSize(), new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				Assert.notNull(bean.getUserId(), new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				
				Attachment record = new Attachment();
				BeanUtils.copyProperties(bean, record);
				Date curr = new Date();
				record.setUploadDate(curr);
				record.setUpdateTime(curr);
				return attachmentDao.insertSelective(record);
			}

			
			public AttachmentBean selectByPrimaryKey(Integer id) throws AttachmentException{
				Attachment record = attachmentDao.selectByPrimaryKey(id);
				Assert.notNull(id, new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				if(record==null)
					return null;
				AttachmentBean bean = new AttachmentBean();
				BeanUtils.copyProperties(record, bean);
				return bean;
			}

			
			public int updateByPrimaryKeySelective(AttachmentBean bean) throws AttachmentException{
				Assert.notNull(bean, new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				Assert.notNull(bean.getId(), new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				
				Attachment record = new Attachment();
				BeanUtils.copyProperties(bean, record);
				Date curr = new Date();
				record.setUpdateTime(curr);
				return attachmentDao.updateByPrimaryKeySelective(record);
			}

			
			public List<AttachmentBean> queryAttachmentBeansByIds(Integer[] ids) throws AttachmentException{
				Assert.notNull(ids, new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				Assert.notNull(ids.length<1, new AttachmentException(CommonMessage.COMMON_PARAM_EMPTY));
				String temp = "";
				for (Integer id : ids) {
					temp = temp+","+id;
				}
				temp = "{"+temp.substring(1)+"}";
				List<Attachment> list = attachmentDao.queryAttachmentsByIds(temp);
				List<AttachmentBean> listBean = null;
				if(list!=null&&list.size()>0){
					listBean = new ArrayList<AttachmentBean>();
					for (Attachment attachment : list) {
						AttachmentBean bean = new AttachmentBean();
						BeanUtils.copyProperties(attachment, bean);
						listBean.add(bean);
					}
				}
				return listBean;
			}

}